/***************************************************************************
 * 文件名：FMHostConnnect
 * 功能：
 * 说明：
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/15 19:07:14
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace BuiltInTools
{
	public partial class FMHostConnnect : XtraForm
	{
		/// <summary>
		/// 连接字符串
		/// </summary>
		public string ConnectString
		{
			get
			{
				return string.Format("Data Source={0};User={1};Password={2};MultipleActiveResultSets=true", txtHostName.Text, txtUserName.Text, txtPassword.Text);
			}
			private set {; }
		}

		/// <summary>
		/// 连接名
		/// </summary>
		public string ConnnectName
		{
			get
			{
				return txtConnectName.Text;
			}
			private set {; }
		}

		/// <summary>
		/// 已存在连接名称
		/// </summary>
		private List<string> _nameList = new List<string>();

		public FMHostConnnect(List<string> nameList)
		{
			InitializeComponent();
			_nameList = nameList;
		}

		/// <summary>
		/// 测试连接
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnTestConnect_Click(object sender, EventArgs e)
		{
			try
			{
				if (!TestConectHost())
				{
					return;
				}

				MessageBox.Show("连接成功");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 测试连接主机
		/// </summary>
		private bool TestConectHost()
		{
			try
			{
				string connectString = string.Format("Data Source={0};User={1};Password={2};MultipleActiveResultSets=true", txtHostName.Text, txtUserName.Text, txtPassword.Text);
				CodeGeneratorUtil.TestConnectHost(connectString);
				return true;
			}
			catch (Exception ex)
			{
				MessageBox.Show("连接失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
		}

		/// <summary>
		/// 确认
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnConfirm_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(txtConnectName.Text))
			{
				MessageBox.Show("连接名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			if (string.IsNullOrEmpty(txtHostName.Text))
			{
				MessageBox.Show("主机不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			if (string.IsNullOrEmpty(txtHostName.Text))
			{
				MessageBox.Show("主机不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			if (string.IsNullOrEmpty(txtPassword.Text))
			{
				MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			if (_nameList.Contains(txtConnectName.Text))
			{
				MessageBox.Show("已经存在相同名称的连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}

			if (!TestConectHost())
			{
				return;
			}

			DialogResult = DialogResult.OK;
		}

		/// <summary>
		/// 关闭
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClose_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
		}
	}
}
